package com.example.a001.part19;

import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.example.a001.R;

public class MainActivity extends AppCompatActivity {
    private CustomFrameLayout container;
    private TextView selectedTextView;

    // Getter方法，允许外部类访问selectedTextView
    public TextView getSelectedTextView() {
        return selectedTextView;
    }

    // Setter方法，用于设置selectedTextView（如果需要）
    public void setSelectedTextView(TextView textView) {
        this.selectedTextView = textView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);

        container = findViewById(R.id.container);
        container.setActivity(this); // 设置CustomFrameLayout的Activity引用

        // 动态创建5个TextView
        int[] colors = {Color.parseColor("#6200EE"), Color.parseColor("#FF0266"), Color.parseColor("#3700B3"), Color.parseColor("#03DAC5"), Color.parseColor("#FF6D00")};
        for (int i = 0; i < 5; i++) {
            TextView textView = new TextView(this);
            textView.setText("Text " + (i + 1));
            textView.setBackgroundColor(colors[i]);
            textView.setTextColor(Color.WHITE);
            textView.setPadding(40, 40, 40, 40);

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT
            );
            params.leftMargin = 100 * i;
            params.topMargin = 100 * i;
            container.addView(textView, params);

            bindLongPressListener(textView);
        }
    }

    private void bindLongPressListener(TextView textView) {
        textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                selectedTextView = (TextView) v;
                // 视觉反馈：缩放动画
                selectedTextView.animate().scaleX(1.1f).scaleY(1.1f).setDuration(100).start();
                return true; // 消费事件，阻止后续点击
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (selectedTextView == null) {
            return super.onTouchEvent(event);
        }

        int screenWidth = getResources().getDisplayMetrics().widthPixels;
        int screenHeight = getResources().getDisplayMetrics().heightPixels;

        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_MOVE:
                // 计算新坐标（限制边界）
                float newX = event.getRawX() - selectedTextView.getWidth() / 2f;
                float newY = event.getRawY() - selectedTextView.getHeight() / 2f;
                newX = Math.max(0, Math.min(newX, screenWidth - selectedTextView.getWidth()));
                newY = Math.max(0, Math.min(newY, screenHeight - selectedTextView.getHeight()));

                // 更新位置
                selectedTextView.setX(newX);
                selectedTextView.setY(newY);
                break;
            case MotionEvent.ACTION_UP:
                // 释放时恢复状态
                selectedTextView.animate().scaleX(1f).scaleY(1f).setDuration(100).start();
                selectedTextView = null;
                break;
        }
        return true;
    }
}
